<?php
/*
 * [佳喜网络!] (C)2008-2009 JiaXi Inc.
 * $Id: include.php 2008-8-13 19:28:02 pepsi.zh $
 * -------------------------------------------
 * 网址：www.he88.cn
 * E-mail：pepsi_zh AT yeah.net
 * -------------------------------------------
 * 这是一个共享软件，遵循我们公司的共享协议。
*/
interface SiteDbInter{
	### 功能：用户登陆判断  参数：$post 记录值；#####
    function addRecord($post);
    ### 功能：get网站列表for SELECT 参数：$userid 用户id; $type 返回值类型 $cataname 是否带类别名;
    function getRecord($userid,$cataname=true,$type=true);
    ### 功能：得到一条记录; 参数：$id 记录id; $type 操作类型，当为ture时，必须带$userid参数 ######
    function getOneRecord($id,$type=false,$userid='');
    ### 功能：删除一条站点记录 ; 参数：$id 记录id 可以是一条数据也可以是id数组；$type 操作类型，当为ture时，必须带$userid参数 ######
    function deleteRecord($id,$type=false,$userid='');
}
class site implements SiteDbInter {
	public $error = array();
	public $action = '提交';
	function addRecord($post){
		global $dbtable,$dbconn;
		$flag = true;
		$var = array();
		$post = specialhtml($post,'zcontent');
		if (empty($post[zname])){
			$flag = false;
			$this->error[zname_error] = '网站名称不能为空';
		}else {
			$var[zname] = $post[zname];
		}
		$post[zurl] = strtolower($post[zurl]);
		$varlongth = strlen($post[zurl]);
		if($varlongth < 3){
			$flag = false;
			$this->error[zurl_error] = '网站地址有误';
		}elseif ($varlongth > 100){
			$flag = false;
			$this->error[zurl_error] = '网站地址长度不能超过100字节';
		}else {
			if (!preg_match(PREG_URL,$post[zurl])){
				$this->error[zurl_error] = '网站地址不正确';
			}else{
				$var[zurl] = $post[zurl];
			}
		}
		if (empty($post[zbelongid])){
			$flag = false;
			$this->error[zbelongid_error] = '网站类别不能为空';
		}else {
			$var[zbelongid] = $post[zbelongid];
		}
		$varlongth = strlen($post[zcontent]);
		if ($varlongth < 50) {
			$flag = false;
			$this->error[zcontent_error] = '网站内容不能小于50字节';
		}elseif ($varlongth > 2000){
			$flag = false;
			$this->error[zcontent_error] = '网站内容不能多余2000字节';
		}else {
			$var[zcontent] = $post[zcontent];
		}
		if (empty($post[userId])){
			header('location:/member/login.php');
			exit;
		}else {
			$var[zuserid] = $post[userId];
		}
		if (isset($post[zheadtitle])){
			$var[zheadtitle] = $post[zheadtitle];
		}
		if (isset($post[zheadkeyword])){
			$var[zheadkeyword] = $post[zheadkeyword];
		}
		if (isset($post[zheaddescrib])){
			$var[zheaddescrib] = $post[zheaddescrib];
		}
		if ($flag){
			$var[zdate] = time();
			$var[ztitle] = $post[ztitle];
			$var[zkeyword] = $post[zkeyword];
			$var[zcheck] = 'n';
			$var[zstatic] = 'n';
			if ($post[zwebid]){
				$this->action = '修改';
				return $dbconn->updateData($var,$dbtable->site,array('zwebid'=>$post[zwebid],'zuserid'=>$post[userId]));
			}else {
				$this->action = '添加';
				return $dbconn->addData($var,$dbtable->site);
			}
		}else {
			return false;
		}
	}
	function getRecord($userid,$cataname=true,$type=true){
		global $dbtable,$dbconn;
		if (empty($userid)){
			return false;
		}
		if ($type){
			$query = "select zsiteid,zname from $dbtable->site where zuserid=$userid and zcheck='y' order by zsiteid desc";
			return $dbconn->Fetcharray_area($query,'zsiteid','zname');
		}else {
			if($cataname){
				$query = "select a.*,b.zname as zbelongname from $dbtable->site a left join $dbtable->belong b on (a.zbelongid=b.zbelongid) where a.zuserid=$userid order by a.zsiteid desc";
			}else{
				$query = "select * from $dbtable->site where zuserid=$userid order by zsiteid desc";
			}
			return $dbconn->Fetcharraynum($query);
		}
	}
	function getOneRecord($id,$type=false,$userid=''){
		global $dbtable,$dbconn;
		if (empty($id)){
			return false;
		}else{
			$where = "where zsiteid=$id";
		}
		if ($type){
			if (empty($userid)){
				return false;
			}else {
				$where .= " and zuserid=$userid";
			}
		}
		$query = "select * from $dbtable->site $where limit 1";
		return $dbconn->Getonerecord($query);
	}
	function deleteRecord($id,$type=false,$userid=''){
		global $dbtable,$dbconn;
		if (empty($id)){
			return false;
		}else {
			$where_id = "zsiteid in (".explode(',',$id).")";
		}
		if ($type){
			if (empty($userid)){
				return false;
			}else {
				$where_id .= " and zuserid=$userid";
			}
		}
		$query = "delete from $dbtable->site $where_id";
		$dbconn->Query($query);
		return $dbconn->returnAffected();
	}
}

?>